﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView.EmpBasicManager
{
    public partial class EmpAssessmentWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private EmpbasicService.Emp_AssessmentModel model = new EmpbasicService.Emp_AssessmentModel();
        private EmpbasicService.Emp_AssessmentModel oldmodel = new EmpbasicService.Emp_AssessmentModel();
        private InsertSystemRemark insertsys = new InsertSystemRemark();
        public int ID;
        public int EmpID;
        public bool IsNew = false;
        public EmpAssessmentWindow(int empID, int iD, bool isnew)
        {
            InitializeComponent();
            ID = iD;
            EmpID = empID;
            IsNew = isnew;
            if (IsNew)
            {
                this.Title = "新增考核信息";
                model = new EmpbasicService.Emp_AssessmentModel();
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑考核信息";
                Client.GetEmp_AssessmentModelCompleted += new EventHandler<EmpbasicService.GetEmp_AssessmentModelCompletedEventArgs>(Client_GetEmp_AssessmentModelCompleted);
                Client.GetEmp_AssessmentModelAsync("(" + EmpID + ")", ID);
            }
        }

        void Client_GetEmp_AssessmentModelCompleted(object sender, EmpbasicService.GetEmp_AssessmentModelCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                model = e.Result[0];
                oldmodel = new EmpbasicService.Emp_AssessmentModel
                {
                    AssessmentType = model.AssessmentType,
                    AssessmentResult = model.AssessmentResult,
                    AssessmentDate = model.AssessmentDate
                };
            }
            this.DataContext = model;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (IsNew == true)
            {
                model.EmpBasicID = EmpID;

                Client.AddEmp_AssessmentModelCompleted += new EventHandler<EmpbasicService.AddEmp_AssessmentModelCompletedEventArgs>(Client_AddEmp_AssessmentModelCompleted);
                Client.AddEmp_AssessmentModelAsync(model);
            }
            else
            {
                if (!string.IsNullOrEmpty(begin.Text)) model.AssessmentDate = Convert.ToDateTime(begin.Text);
                else
                    model.AssessmentDate = null;
                Client.UpdateEmp_AssessmentModelCompleted += new EventHandler<EmpbasicService.UpdateEmp_AssessmentModelCompletedEventArgs>(Client_UpdateEmp_AssessmentModelCompleted);
                Client.UpdateEmp_AssessmentModelAsync(model);
            }
        }

        void Client_UpdateEmp_AssessmentModelCompleted(object sender, EmpbasicService.UpdateEmp_AssessmentModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！"); 
                InsertSystem("编辑");
                this.DialogResult = true;
            }
        }

        void Client_AddEmp_AssessmentModelCompleted(object sender, EmpbasicService.AddEmp_AssessmentModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！"); 
                InsertSystem("新增");
                this.DialogResult = true;
            }
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        void InsertSystem(string FangShi)
        {
            if (FangShi == "新增")
            {
                insertsys.Insert("考核情况", "", "", model.ID, FangShi, "");
            }
            else
            {
                if (oldmodel.AssessmentDate != model.AssessmentDate)
                    insertsys.Insert("考核情况子集", oldmodel.AssessmentDate == null ? null : Convert.ToDateTime(oldmodel.AssessmentDate).ToShortDateString(),
                        model.AssessmentDate == null ? null : Convert.ToDateTime(model.AssessmentDate).ToShortDateString(),
                        model.ID, FangShi, "考核时间");
                if (oldmodel.AssessmentType != model.AssessmentType)
                    insertsys.Insert("考核情况子集", oldmodel.AssessmentType, model.AssessmentType,
                        model.ID, FangShi, "考核类别");
                if (oldmodel.AssessmentResult != model.AssessmentResult)
                    insertsys.Insert("考核情况子集", oldmodel.AssessmentResult, model.AssessmentResult,
                        model.ID, FangShi, "考核结果");
            }

        }
    }
}
